
using Youz.Common.DBUtility;
using Youz.CreditRepay.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.CreditRepay.DAL
{
    /// <summary>
    /// CR_RepaymentPlanItem Data Class
    /// </summary>
    public partial class CR_RepaymentPlanItemDAL : BaseDAL
    {
        private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "CR_RepaymentPlanItem";

        public CR_RepaymentPlanItemDAL()
        { }

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(CR_RepaymentPlanItem model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into [CR_RepaymentPlanItem](");
            strSql.Append("[BankCardID],[PlanID],[ParentID],[Amount],[Date],[Status],[CreatedOn],[ModifiedOn]");
            strSql.Append(") values (");
            strSql.Append("@ItemID,@BankCardID,@PlanID,@ParentID,@Amount,@Date,@Status,@CreatedOn,@ModifiedOn");
            strSql.Append(")");

            SqlParameter[] parameters = {
                  new SqlParameter("@Item",model.ItemID),
                    new SqlParameter("@BankCardID", model.BankCardID),
                    new SqlParameter("@PlanID", model.PlanID),
                    new SqlParameter("@ParentID", model.ParentID),
                    new SqlParameter("@Amount", model.Amount),
                    new SqlParameter("@Date", model.Date),
                    new SqlParameter("@Status", model.Status),
                    new SqlParameter("@CreatedOn", DateTime.Now),
                    new SqlParameter("@ModifiedOn", DateTime.Now)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(CR_RepaymentPlanItem model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [CR_RepaymentPlanItem] set ");
            strSql.Append("[BankCardID]=@BankCardID,[PlanID]=@PlanID,[ParentID]=@ParentID,[Amount]=@Amount,[Date]=@Date,[Status]=@Status,[CreatedOn]=@CreatedOn,[ModifiedOn]=@ModifiedOn");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {

                    new SqlParameter("@ID", model.ID),
                    new SqlParameter("@BankCardID", model.BankCardID),
                    new SqlParameter("@PlanID", model.PlanID),
                    new SqlParameter("@ParentID", model.ParentID),
                    new SqlParameter("@Amount", model.Amount),
                    new SqlParameter("@Date", model.Date),
                    new SqlParameter("@Status", model.Status),
                    new SqlParameter("@ModifiedOn", DateTime.Now)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Int32 ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [CR_RepaymentPlanItem] ");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {

                new SqlParameter("@ID", ID)

            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool DeleteByPlanID(Int32 planID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [CR_RepaymentPlanItem] ");
            strSql.Append(" where [PlanID]=@ID");

            SqlParameter[] parameters = {

                new SqlParameter("@ID", planID)

            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CR_RepaymentPlanItem Get(Int32 ID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 * from [CR_RepaymentPlanItem] ");
            strSql.Append(" where [ID]=@ID");

            SqlParameter[] parameters = {

                new SqlParameter("ID", ID)

            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CR_RepaymentPlanItem Get(DataRow row)
        {
            if (row == null) return null;

            CR_RepaymentPlanItem model = new CR_RepaymentPlanItem();


            if (row["ID"] != DBNull.Value)
            {
                model.ID = (Int32)row["ID"];
            }

            if (row["ItemID"] != DBNull.Value)
            {
                model.ItemID = (Int32)row["ItemID"];
            }

            if (row["BankCardID"] != DBNull.Value)
            {
                model.BankCardID = (Int32)row["BankCardID"];
            }

            if (row["PlanID"] != DBNull.Value)
            {
                model.PlanID = (Int32)row["PlanID"];
            }

            if (row["ParentID"] != DBNull.Value)
            {
                model.ParentID = (Int32)row["ParentID"];
            }

            if (row["Amount"] != DBNull.Value)
            {
                model.Amount = (Decimal)row["Amount"];
            }

            if (row["Date"] != DBNull.Value)
            {
                model.Date = (DateTime)row["Date"];
            }

            if (row["Status"] != DBNull.Value)
            {
                model.Status = (Int32)row["Status"];
            }

            if (row["CreatedOn"] != DBNull.Value)
            {
                model.CreatedOn = (DateTime)row["CreatedOn"];
            }

            if (row["ModifiedOn"] != DBNull.Value)
            {
                model.ModifiedOn = (DateTime)row["ModifiedOn"];
            }


            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * from [CR_RepaymentPlanItem] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }            

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [CR_RepaymentPlanItem] ");

            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [CR_RepaymentPlanItem] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [CR_RepaymentPlanItem]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by ID desc ", out count, cmdParms);
            total = count;
            return dt;
        }
        #endregion  成员方法
    }
}


